package com.peng.leetcode.array;

/**
 * SearchMatrix
 * <p>
 * 240. 搜索二维矩阵 II
 *
 * @author: lupeng6
 * @create: 2021/3/19 14:01
 */
public class SearchMatrix1_1 {

    public static void main(String[] args) {
        int[][] matrix = {{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}};
        System.out.println(new SearchMatrix1_1().searchMatrix(matrix, 5));
    }


    /**
     * 使用二分法查找指定数据
     *
     * @author lupeng6
     * @date 2021/3/22 10:55
     */
    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0) {
            return false;
        }
        int row = matrix.length - 1;
        int col = 0;
        while (row >= 0 && col < matrix[0].length) {
            if (matrix[row][col] > target) {
                row--;
            } else if (matrix[row][col] < target) {
                col++;
            } else {
                return true;
            }
        }
        return false;
    }



}
